import Vue from 'vue'
import Router from 'vue-router'
import { getUserInfo } from 'vue-xiaobu-utils'
import ConfirmBox from '../components/confirm/ConfirmBox.js'
import { wxLogin } from '../utils/wxLogin.js'
// 登录页
import loginIndex from '@/wx/customizedShuttleBus/pages/login/index'
// 定制线路
import buyIndex from '@/wx/customizedShuttleBus/pages/buy/index'
import buyTicket from '@/wx/customizedShuttleBus/pages/buy/buyTicket'
import buyOrder from '@/wx/customizedShuttleBus/pages/buy/createOrder'
import paymentResult from '@/wx/customizedShuttleBus/pages/buy/paymentResult'
// 我的车票
import myIndex from '@/wx/customizedShuttleBus/pages/my/index'
import failureTicket from '@/wx/customizedShuttleBus/pages/my/failureTicket'
import myTicket from '@/wx/customizedShuttleBus/pages/my/ticketInfo'
import myRemark from '@/wx/customizedShuttleBus/pages/my/remark'
// 购票记录
import recordIndex from '@/wx/customizedShuttleBus/pages/record/index'
import recordDetails from '@/wx/customizedShuttleBus/pages/record/details'
// 地图
import map from '@/wx/customizedShuttleBus/pages/map'
// 定制班线意见征集
import nonstopLIne from '@/wx/customizedShuttleBus/pages/nonstopLIne/customBus/App'
import chooseSite from '@/wx/customizedShuttleBus/pages/nonstopLIne/chooseSite/App'
import mapChooseSite from '@/wx/customizedShuttleBus/pages/nonstopLIne/mapChooseSite/App'
// 用户协议、购票说明
import userRule from '@/wx/customizedShuttleBus/pages/userRule'
import ticketRule from '@/wx/customizedShuttleBus/pages/ticketRule'
Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      name: 'loginIndex',
      component: loginIndex,
      meta: {
        title: '登录',
        keepAlive: true
      }
    },
    {
      path: '/buy/index',
      name: 'buyIndex',
      component: buyIndex,
      meta: {
        title: '定制班线'
      }
    },
    {
      path: '/buy/buyTicket',
      name: 'buyTicket',
      component: buyTicket,
      meta: {
        title: ''
      }
    },
    {
      path: '/buy/createOrder',
      name: 'createOrder',
      component: buyOrder,
      meta: {
        title: '订单确认'
      }
    },
    {
      path: '/buy/paymentResult',
      name: 'paymentResult',
      component: paymentResult,
      meta: {
        title: '购票结果'
      }
    },
    {
      path: '/my/index',
      name: 'myIndex',
      component: myIndex,
      meta: {
        title: '我的车票'
      }
    },
    {
      path: '/my/failureTicket',
      name: 'failureTicket',
      component: failureTicket,
      meta: {
        title: '失效票'
      }
    },
    {
      path: '/my/ticket',
      name: 'myTicket',
      component: myTicket,
      meta: {
        title: '123路'
      }
    },
    {
      path: '/my/remark',
      name: 'myRemark',
      component: myRemark,
      meta: {
        title: '使用说明'
      }
    },
    {
      path: '/record/index',
      name: 'recordIndex',
      component: recordIndex,
      meta: {
        title: '购票记录'
      }
    },
    {
      path: '/record/details',
      name: 'recordDetails',
      component: recordDetails,
      meta: {
        title: '详情'
      }
    },
    {
      path: '/map',
      name: 'map',
      component: map,
      meta: {
        title: '地图'
      }
    },
    {
      path: '/nonstopLIne/customBus/App',
      name: 'customBusHome',
      component: nonstopLIne,
      meta: {
        keepAlive: true,
        title: '意见征集'
      }
    },
    {
      path: '/nonstopLIne/chooseSite/App',
      name: 'chooseSite',
      component: chooseSite,
      meta: {
        keepAlive: true,
        title: '选择地点'
      }
    },
    {
      path: '/nonstopLIne/mapChooseSite/App',
      name: 'mapChooseSite',
      component: mapChooseSite,
      meta: {
        keepAlive: true,
        title: '地图选点'
      }
    },
    {
      path: '/userRule',
      name: 'userRule',
      component: userRule,
      meta: {
        title: '用户协议'
      }
    },
    {
      path: '/ticketRule',
      name: 'ticketRule',
      component: ticketRule,
      meta: {
        title: '购票说明'
      }
    }
  ]
})

router.beforeEach((to, from, next) => {
  // 登录、用户协议页面不需要登录
  if (to.name === 'loginIndex' || to.name === 'userRule') {
    next()
  } else {
    // 判断是否需要登录
    if (parseInt(getUserInfo().IS_LOGIN) === 1) {
      next()
    } else {
      ConfirmBox.show({
        title: '提示',
        message: '您的账号长时间未操作或在其他设备登录，请重试',
        confirmText: '退出登录',
        cancelText: null
      }).then(() => {
        console.log('退出登录~~~~~')
        localStorage.clear()
        next({ name: 'loginIndex' })
        wxLogin(true)
      })
    }
  }
})

export default router
